
//拖拽按钮交互
$(".dd-handle").hide();
var select_val=$("#select-pipeline_number").val();
if(select_val!==''){
    $(".dd-handle").show();
}
$("#select-pipeline_number").on('change',function(){
    var select_val=$(this).val();
    if(select_val!==''){
        $(".dd-handle").show();

    }
});


//修改原有表格样式配合拖拽
$("#builder-table-main").addClass('dd');
$("#builder-table-main").find("tr").addClass('dd-item');
$("#builder-table-main").find("tbody").addClass('dd-list');
var ids=$("#builder-table-main").find("tr").each(function(){
    var id =$(this).find("td.point_id").text();
    $(this).attr('data-id',$.trim(id));
});

//sort数据存储变量
var changedata;
//拖拽插件参数
var options = {
    listNodeName    : 'tbody',
    itemNodeName    : 'tr',
    rootClass       : 'dd',
    listClass       : 'dd-list',
    itemClass       : 'dd-item',
    dragClass       : 'dd-dragel',
    handleClass     : 'dd-handle',
    collapsedClass  : 'dd-collapsed',
    placeClass      : 'dd-placeholder',
    noDragClass     : 'dd-nodrag',
    emptyClass      : 'dd-empty',
    expandBtnHTML   : '<button data-action="expand" type="button">Expand</button>',
    collapseBtnHTML : '<button data-action="collapse" type="button">Collapse</button>',
    group           : 0,
    maxDepth        : 1,
    threshold       : 20
};
//拖拽完成后输出数据
var updateOutput = function(e)
{
    var list   = e.length ? e : $(e.target);
    // output = list.data('output');
    if (window.JSON) {
        changedata=list.nestable('serialize');
        //拖拽交互完成生产保存按钮
        $(".save_sort").remove();
        var save_button='<a title="保存排序" class="save_sort btn btn-success ajax-post confirm" target-form="ids" onclick="save_sort()" href="javascript:;" icon="fa fa-fw fa-arrows"><i class="fa fa-fw fa-arrows"></i> 保存排序</a>';
        $(".sort_reset").before(save_button);
        // console.debug(window.JSON.stringify(list.nestable('serialize')));
    } else {
        console.debug('JSON browser support required for this demo.');
        // output.val();
    }
};

//拖拽初始化
$('#builder-table-main').nestable(options).on('change', updateOutput);

//保存排序
function save_sort(){
    layer.confirm('是否要保存排序？', {
        btn: ['确认','取消'] //按钮
    }, function(){
        $.ajax({
            type: "post",
            data: {
                sort_id:changedata
                // pipeline_number:select_val,
                // business_id:business_id,
                // company_id:company_id
            },
            url: "drag_sort_save",
            async: false,
            dataType: "json",
            success: function(data) {
                data=JSON.parse(data);
                layer.msg(data.msg,function(){
                    if(data.code){
                        window.location.reload()
                    }
                },200);
            }
        });
    });
}

